package com.vol.utils;

import com.vol.JwtHelper;
import com.vol.RedisUtils;
import com.vol.dao.SysUserDao;
import com.vol.entity.SysUser;
import com.vol.entity.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserContext {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private RedisUtils<Object> redisUtils;

    private String Authorization="";

    @Autowired
    private HttpServletRequest request;

    public UserInfo getUserInfo() {
        String authorization = request.getHeader("Authorization");
        String token = authorization.replace("Bearer ","");
        Integer loginUid = jwtHelper.getUserId(token);
        return _GetUserInfo(loginUid);
    }

    private UserInfo _GetUserInfo(int _userId){
        UserInfo _userInfo= new UserInfo();
        SysUser user = sysUserDao.getById(_userId);
        if(user != null){
            _userInfo.setUser_Id(user.getUserId());
            _userInfo.setRole_Id(user.getRoleId());
            _userInfo.setRoleName(user.getRolename());
            _userInfo.setUserName(user.getUsername());
            _userInfo.setUserTrueName(user.getUsertruename());
            _userInfo.setEnable(user.getEnable());
            if(user.getDeptids()!=null && user.getDeptids().length()>0){
                List<String> deptIds= Arrays.asList(user.getDeptids().split(","));
                _userInfo.setDeptIds(deptIds);
            }
            _userInfo.setToken(user.getToken());
        }
        return _userInfo;
    }

}
